import {connect} from 'react-redux'
import {verify} from '../../store/actions/verify'
import {userInfo} from '../../store/actions/userInfo'
import {getMsgList,readChat} from '../../store/actions/chatmsg'
import {useEffect} from 'react'
import {Navigate,Route,Routes,useLocation} from 'react-router-dom'
import {
    NavBar
} from 'antd-mobile'
import {reqGetUser } from '../../api/'
import FootBar from '../../components/FootBar/'
import SeekerInfo from '../../pages/SeekerInfo/'
import BossInfo from '../../pages/BossInfo/'
import Personal from '../Personal/'
import Boss from '../../pages/Boss/';
import Seek from '../../pages/Seek/';
import Message from '../Message'
import Chat from '../Chat/'

let verify1={v:false}
let verifyPath=''
 
function  Main(props){
    
    const  {readChat,getMsgList}=props
    const tabs = [
        {
           key: 'laoban',
           title: '老板',
           icon: "laoban",
           hidden:true
        },
       {
         key: 'dashen',
         title: '大神',
         icon:'dashen',
         hidden:true
       },
       {
         key: 'message',
         title: '消息',
         icon: 'message',
         badge: '0',
       },
       {
         key: 'personal',
         title: '个人',
         icon: 'personal',
       },
     ]  
    const  location=useLocation() 
    const path= location.pathname
    let navName=''
    tabs.forEach((item)=>{
        if('/main/'+item.key===path){
            navName=item.title
        }
    })

    if(verifyPath==='laoban'){
        tabs[0].hidden=false
    }else{
        tabs[1].hidden=false
    }

    
    console.log(location)
   
    //发请求等到userinfo信息
    const setUserInfo=props.setUserInfo
    useEffect(()=>{
        console.log('执行几遍')
        getMsgList()
        readChat()
        reqGetUser().then((response)=>{
            console.log(response)
            setUserInfo({info:response.data,isUserInfo:true})
            },()=>{ 
            //失败
        })
    },[setUserInfo])
    if(!props.userInfo.info&&!props.userInfo.isUserInfo){
        return null
    }
    if(props.userInfo.info){
        if(verify1.v){
            return(
                <div>
                    {
                       navName? <NavBar className="navbar" backArrow={false}>{navName}列表</NavBar>:''
                    }
                   <div style={{marginTop:'45px'}}>
                       <Routes>
                            <Route path='bossInfo'  element={<BossInfo></BossInfo>}/>
                            <Route path='seekInfo'  element={<SeekerInfo></SeekerInfo>}/>
                            <Route path='personal'  element={<Personal verify={verify1}></Personal>}/>
                            <Route path='laoban'  element={<Boss></Boss>}/>
                            <Route path='dashen'  element={<Seek></Seek>}/>
                            <Route path='message'  element={<Message></Message>}/> 
                            <Route path="chat/:id/:name" element={<Chat></Chat>}></Route>
                        </Routes> 
                   </div>
                    {
                       navName? <FootBar tabs={tabs}></FootBar>:''
                    }
                    
                </div>  
            )
        }else{ 
                verify1.v=true
                if(props.userInfo.info.header){
                    if(props.userInfo.info.type==='laoban'){
                        verifyPath='dashen'
                        console.log('跳大神')
                        return (<Navigate to="dashen" replace></Navigate>) 
                    }else{
                        verifyPath='laoban'
                        console.log('跳老板')
                        return (<Navigate to="laoban" replace></Navigate>)
                    }    
                }else{
                    if(props.userInfo.info.type==='laoban'){
                        return (<Navigate to="bossInfo" replace></Navigate>)
                    }else{
                        return (<Navigate to="seekInfo" replace></Navigate>)
                    } 
                }       
        }
        
    }else{
        return (<Navigate to="/login"></Navigate>)
    }
        
}

export default connect(
state=>(
    {
    verify:state.verify,
    userInfo:state.userInfo
    }),
    {
        setVerify:verify,
        setUserInfo:userInfo,
        getMsgList,
        readChat
    })(Main)

        
        